<template>
  <div>
    <el-button @click="handleClick" type="primary">
      <slot></slot>
    </el-button>
    <el-dialog class="m-choose-icon" :title="title" v-model="dialogVisible" @close="handleClose">
      <div class="container">
        <div class="containerItem" v-for="(item,index) in Object.keys(ElIcons)" :key="index">
          <div @click="clickItem(item)">
            <component :is='`el-icon-${toLine(item)}`'></component>
          </div>
          <div class="icon">{{ item }}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { watch,ref } from "vue";
import * as ElIcons from "@element-plus/icons-vue";
import { toLine } from "@/utils";
import { useCopy } from "@/hooks/useCopy";
let props = defineProps({
  title: String,
  visible: {
    type: Boolean,
    default: false,
  },
});
// 拷贝父组件传过来的visible
const dialogVisible = ref(props.visible);
const emits = defineEmits(["update:visible"]);
// 点击按钮打开弹窗
const handleClick = () => {
  emits("update:visible", !props.visible);
};
// 点击图标复制代码
let clickItem = (item: string) => {
  useCopy(`<el-icon-${toLine(item)} />`)
  dialogVisible.value = false
}
// 监听visible的变化
watch(
  () => props.visible,
  (newVal) => {
    console.log(newVal);
    dialogVisible.value = newVal;
  }
);
// 关闭弹窗
const handleClose = () => {
  emits("update:visible", false);
};
</script>

<style scoped lang="scss">
.container{
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.containerItem{
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  cursor: pointer;
}
svg{
  width: 2em;
  height: 2em;
}
</style>
